package com.longge;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-04-26 01:09
 */
public class MakeConnected {
    public int makeConnected(int n, int[][] connections) {

        int m = connections.length;
        if (m < n - 1) return -1;
        UF uf = new UF(n);
        for (int[] connection : connections) {
            uf.union(connection[0], connection[1]);
        }
        return uf.count() - 1;
    }
}
